package com.springboot.frame.plugin;

import org.apache.ibatis.reflection.SystemMetaObject;

import java.lang.reflect.Proxy;

/**
 * mybatis的插件工具类
 *
 * @author liheng
 */
public final class PluginUtils {

  private PluginUtils() {
  }

  /**
   * 得到真实代理对象
   *
   * @param target 目标
   * @return 真实代理对象
   */
  public static Object realTarget(Object target) {
    if (Proxy.isProxyClass(target.getClass())) {
      return realTarget(SystemMetaObject.forObject(target).getValue("h.target"));
    }
    return target;
  }

  public static Object realStatement(Object target) {
    if (Proxy.isProxyClass(target.getClass())) {
      return realStatement(SystemMetaObject.forObject(target).getValue("h.statement"));
    }
    return target;
  }

}
